package com.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
public class VideoApiImpl implements VideoApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Map<String,Object> queryVideoList(Integer uid, Integer page, Integer pageSize) {
        Query query1 = Query.query(Criteria.where("userId").is(uid));
        int count = (int)mongoTemplate.count(query1, "video");
        Pageable pageable = PageRequest.of(page-1,pageSize);
        Query query = Query.query(Criteria.where("userId").is(uid)).with(pageable);
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        Map<String,Object> map = new HashMap<>();
        map.put("count",count);
        map.put("videoList",videoList);
        return map;
    }
}
